package dsk.demo.product.controller.supplier;

import com.baomidou.mybatisplus.core.metadata.IPage;
import dsk.demo.common.utils.R;
import dsk.demo.product.dto.SupplierQueryDto;
import dsk.demo.product.entity.SupplierEntity;
import dsk.demo.product.service.SupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("supplier/supplier")
public class SupplierController {

    @Autowired
    SupplierService supplierService;

    /**
     * 新增
     */
    @RequestMapping("/save")
    public R save(@RequestBody SupplierEntity entity){
        try {
            supplierService.save(entity);
        }catch (Exception e){
            System.out.println(e.getLocalizedMessage());
            return R.error(501,"新增供应商失败");
        }
        return R.ok();
    }

    /**
     * 单个查询
     */
    @RequestMapping("/info/{id}")
    public R save(@PathVariable("id") Integer id){
        try {
            SupplierEntity byId = supplierService.getById(id);
            return R.ok().put("supplierInfo",byId);
        }catch (Exception e){
            System.out.println(e.getLocalizedMessage());
            return R.error(501,"新增供应商失败");
        }
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] ids){
        try {
            List<SupplierEntity> suppliers = new ArrayList<>();
            for (Integer id : ids) {
                SupplierEntity supplierEntity = new SupplierEntity();
                supplierEntity.setId(id);
                supplierEntity.setStatus("1");
                suppliers.add(supplierEntity);
            }
            supplierService.updateBatchById(suppliers);
        }catch (Exception e){
            System.out.println(e.getLocalizedMessage());
            return R.error(501,"删除供应商失败");
        }
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody SupplierEntity entity){
        try {
            supplierService.updateById(entity);
        }catch (Exception e){
            System.out.println(e.getLocalizedMessage());
            return R.error(501,"删除供应商失败");
        }
        return R.ok();
    }

    /**
     * 按条件查询
     */
    @RequestMapping("/list")
    public R list(@RequestBody SupplierQueryDto dto){
        try {
            IPage<SupplierEntity> supplierEntityIPage = supplierService.queryByPage(dto);
            return R.ok().put("page",supplierEntityIPage);
        }catch (Exception e){
            System.out.println(e.getLocalizedMessage());
            return R.error(501,"查询失败");
        }
    }

    /**
     * 按条件查询
     */
    @GetMapping("/list")
    public R list(String keyWord){
        try {
            List<SupplierEntity> supplierList = supplierService.queryByKeyWord(keyWord);
            return R.ok().put("list",supplierList);
        }catch (Exception e){
            System.out.println(e.getLocalizedMessage());
            return R.error(501,"查询失败");
        }
    }

}
